//
//  JBWelcomeViewController.swift
//  sinaBlog
//
//  Created by jobo on 15/10/31.
//  Copyright © 2015年 jobo. All rights reserved.
//

import UIKit

class JBWelcomeViewController: UIViewController {

    
    
    var iconViewBottmContain : NSLayoutConstraint?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        

        
        prepareforUI()
        
        if let urlString = JBUserAccount.loadAccount()?.iconImg
        {
        
            iconImgView.setImageWithURL(NSURL(string : urlString)!, placeholderImage: UIImage(named: "avatar_default_big"))
            
        }
        
        print("\(backgroundImgView.center.x),\(backgroundImgView.center.y)")
        
        print("\(iconImgView.frame.origin.x),\(iconImgView.frame.origin.y)")
        
        print((iconViewBottmContain?.constant))
        // Do any additional setup after loading the view.
        
        
        view.backgroundColor = UIColor.whiteColor()
        
        
        
        
        
       
        print((iconViewBottmContain?.constant))
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //弹簧0-1,初始速度,
        UIView.animateWithDuration(1.0, delay: 2, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.iconViewBottmContain?.constant = -(UIScreen.mainScreen().bounds.height - 160)
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1.0, animations: { () -> Void in
                    self.textLabel.alpha = 1
                    }, completion: { (_) -> Void in
                         (UIApplication.sharedApplication().delegate as! AppDelegate).switchController(true)
                })
        }
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    ///准备ui界面
    func prepareforUI()
    {
        //添加控件
        view.addSubview(backgroundImgView)
        view.addSubview(iconImgView)
        view.addSubview(textLabel)
        
             //添加约束
        
        //背景图片约束
        backgroundImgView.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraints( NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[imgsubView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["imgsubView": backgroundImgView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[imgsubView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["imgsubView": backgroundImgView]))
        
      //头像图片约束
       iconImgView.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraint(NSLayoutConstraint(item: iconImgView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: backgroundImgView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        iconViewBottmContain = NSLayoutConstraint(item: iconImgView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: backgroundImgView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        
        
        //设置宽的约束
        view.addConstraint(NSLayoutConstraint(item: iconImgView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        //设置高的约束
        view.addConstraint(NSLayoutConstraint(item: iconImgView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        view.addConstraint(iconViewBottmContain!)
        

        
        
        
        
        //文字约束
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImgView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        
        
    }
    
    
    //懒加载欢迎界面视图
    
    lazy var backgroundImgView : UIImageView =
    {
       
        let backgroundImgView = UIImageView(image: UIImage(named: "ad_background"))
        return backgroundImgView
    }()
    
    lazy var iconImgView : UIImageView =
    {
        let iconImgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //设置图片是圆型
        iconImgView.layer.cornerRadius = 42.5
        iconImgView.layer.masksToBounds = true
        
        return iconImgView
    }()
    
    lazy var textLabel : UILabel =
    {
        let textLabel = UILabel()
        textLabel.text = "欢迎回来"
        textLabel.alpha  =  0
        return textLabel
    }()
    
  
    
    
    
    
    
   
}
